#pragma once
#include <iostream>
#include <string>
#include <cstdio>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <stdarg.h>
enum Level
{
    DEBUG = 0,
    INFO,
    WARNING,
    ERROR,
    FATAL
};
std::string levelToString(int level)
{
    switch (level)
    {
    case DEBUG:
        return "debug";
    case INFO:
        return "info";
    case WARNING:
        return "warning";
    case ERROR:
        return "error";
    case FATAL:
        return "fatal";
    }
    return "none";
}
std::string GetTimeString()
{
    time_t cur_time = time(nullptr);
    struct tm *format_time = localtime(&cur_time);
    if (format_time == NULL)
    {
        return "None";
    }
    char time_buffer[1024];
    snprintf(time_buffer, sizeof(time_buffer), "%0d-%0d-%0d %0d:%0d:%0d",
             format_time->tm_year + 1900,
             format_time->tm_mon + 1,
             format_time->tm_mday,
             format_time->tm_hour,
             format_time->tm_min,
             format_time->tm_sec);
    return time_buffer;
}
// 日志格式： 等级 + 时间 + 代码所在文件/行数 日志内容
void LogMessage(std::string filename, int line, int level, const char *format, ...)
{
    std::string levelstr = levelToString(level);
    std::string timestr = GetTimeString();
    // std::cout << levelstr << " : " << timestr << std::endl;
    pid_t selfid = getpid();

    char buffer[1024];
    va_list arg;
    va_start(arg,format);
    vsnprintf(buffer,sizeof(buffer),format,arg);
    va_end(arg);
    // std::cout << levelstr << " : " << timestr << filename << " : " << line << buffer << std::endl;

    std::cout <<"["<<timestr<<"]"
    <<"["<<levelstr<<"]"
    <<"[pid:"<<selfid<<"]"
    <<"["<<filename<<"]"
    <<"["<<line<<"]"
    <<buffer<<std::endl;
}
void Test(int num,...)
{
    va_list arg;
    va_start(arg,num);
    while(num)
    {

    }
    va_end(arg);
}
#define LOG(level,format,...) LogMessage(__FILE__, __LINE__, level,format, ##__VA_ARGS__)
